const app=getApp()
const userDB=wx.cloud.database().collection("user")
const recordsDB=wx.cloud.database().collection("records")

var o = [ {
  img: "cloud://xiaotiao-6gcr9hxy3d8aa90f.7869-xiaotiao-6gcr9hxy3d8aa90f-1309378747/助眠减压/鸟.jpg",
  name: "鸟鸣",
  src: "cloud://xiaotiao-6gcr9hxy3d8aa90f.7869-xiaotiao-6gcr9hxy3d8aa90f-1309378747/助眠减压/音乐/Bird.wav",
  type: "助眠"
}, {
  img: "cloud://xiaotiao-6gcr9hxy3d8aa90f.7869-xiaotiao-6gcr9hxy3d8aa90f-1309378747/助眠减压/雷.jpg",
  name: "雷声",
  src: "cloud://xiaotiao-6gcr9hxy3d8aa90f.7869-xiaotiao-6gcr9hxy3d8aa90f-1309378747/助眠减压/音乐/thunder1.mp3",
  type: "减压"
}, {
  img: "cloud://xiaotiao-6gcr9hxy3d8aa90f.7869-xiaotiao-6gcr9hxy3d8aa90f-1309378747/助眠减压/雨.jpg",
  name: "雨滴",
  src: "cloud://xiaotiao-6gcr9hxy3d8aa90f.7869-xiaotiao-6gcr9hxy3d8aa90f-1309378747/助眠减压/音乐/Rain.aac",
  type: "减压"
}, {
  img: "cloud://xiaotiao-6gcr9hxy3d8aa90f.7869-xiaotiao-6gcr9hxy3d8aa90f-1309378747/助眠减压/小溪流.jpg",
  name: "流水",
  src: "cloud://xiaotiao-6gcr9hxy3d8aa90f.7869-xiaotiao-6gcr9hxy3d8aa90f-1309378747/助眠减压/音乐/brook1.mp3",
  type: "助眠"
}, {
  img: "cloud://xiaotiao-6gcr9hxy3d8aa90f.7869-xiaotiao-6gcr9hxy3d8aa90f-1309378747/助眠减压/森林.jpg",
  name: "山林",
  src: "cloud://xiaotiao-6gcr9hxy3d8aa90f.7869-xiaotiao-6gcr9hxy3d8aa90f-1309378747/助眠减压/音乐/summer_hill2.mp3",
  type: "减压"
}, {
  img: "cloud://xiaotiao-6gcr9hxy3d8aa90f.7869-xiaotiao-6gcr9hxy3d8aa90f-1309378747/助眠减压/海岸.jpg",
  name: "海岸",
  src: "cloud://xiaotiao-6gcr9hxy3d8aa90f.7869-xiaotiao-6gcr9hxy3d8aa90f-1309378747/助眠减压/音乐/summer_beach1.mp3",
  type: "助眠"
},
{
  img: "cloud://xiaotiao-6gcr9hxy3d8aa90f.7869-xiaotiao-6gcr9hxy3d8aa90f-1309378747/助眠减压/微波.jpg",
  name: "微波",
  src: "cloud://xiaotiao-6gcr9hxy3d8aa90f.7869-xiaotiao-6gcr9hxy3d8aa90f-1309378747/助眠减压/音乐/small_wave.mp3",
  type: "助眠"
},  {
  img: "cloud://xiaotiao-6gcr9hxy3d8aa90f.7869-xiaotiao-6gcr9hxy3d8aa90f-1309378747/助眠减压/风.jpg",
  name: "风",
  src: "cloud://xiaotiao-6gcr9hxy3d8aa90f.7869-xiaotiao-6gcr9hxy3d8aa90f-1309378747/助眠减压/音乐/wind.mp3",
  type: "助眠"
},  {
  img: "cloud://xiaotiao-6gcr9hxy3d8aa90f.7869-xiaotiao-6gcr9hxy3d8aa90f-1309378747/助眠减压/小溪流.jpg",
  name: "流落",
  src: "cloud://xiaotiao-6gcr9hxy3d8aa90f.7869-xiaotiao-6gcr9hxy3d8aa90f-1309378747/助眠减压/音乐/duster.mp3",
  type: "减压"
},
{
  img: "cloud://xiaotiao-6gcr9hxy3d8aa90f.7869-xiaotiao-6gcr9hxy3d8aa90f-1309378747/助眠减压/风铃.jpg",
  name: "风铃",
  src: "cloud://xiaotiao-6gcr9hxy3d8aa90f.7869-xiaotiao-6gcr9hxy3d8aa90f-1309378747/助眠减压/音乐/door_bell.mp3",
  type: "减压"
}, {
  img: "cloud://xiaotiao-6gcr9hxy3d8aa90f.7869-xiaotiao-6gcr9hxy3d8aa90f-1309378747/助眠减压/沐浴.jpg",
  name: "沐浴",
  src: "cloud://xiaotiao-6gcr9hxy3d8aa90f.7869-xiaotiao-6gcr9hxy3d8aa90f-1309378747/助眠减压/音乐/in_a_bath.mp3",
  type: "减压"
}, {
  img: "cloud://xiaotiao-6gcr9hxy3d8aa90f.7869-xiaotiao-6gcr9hxy3d8aa90f-1309378747/助眠减压/风雪.jpg",
  name: "风雪行",
  src: "cloud://xiaotiao-6gcr9hxy3d8aa90f.7869-xiaotiao-6gcr9hxy3d8aa90f-1309378747/助眠减压/音乐/walking_in_snowstorm.mp3",
  type: "减压"
}, {
  img: "cloud://xiaotiao-6gcr9hxy3d8aa90f.7869-xiaotiao-6gcr9hxy3d8aa90f-1309378747/助眠减压/秋天.jpg",
  name: "秋天街头",
  src: "cloud://xiaotiao-6gcr9hxy3d8aa90f.7869-xiaotiao-6gcr9hxy3d8aa90f-1309378747/助眠减压/音乐/walking_on_a_fall_street.mp3",
  type: "减压"
}, 
{
  img: "cloud://xiaotiao-6gcr9hxy3d8aa90f.7869-xiaotiao-6gcr9hxy3d8aa90f-1309378747/助眠减压/滴水波纹.jpg",
  name: "滴落",
  src: "cloud://xiaotiao-6gcr9hxy3d8aa90f.7869-xiaotiao-6gcr9hxy3d8aa90f-1309378747/助眠减压/音乐/waterdrops.mp3",
  type: "减压"
}, {
  img: "cloud://xiaotiao-6gcr9hxy3d8aa90f.7869-xiaotiao-6gcr9hxy3d8aa90f-1309378747/助眠减压/优美.jpg",
  name: "优美",
  src: "cloud://xiaotiao-6gcr9hxy3d8aa90f.7869-xiaotiao-6gcr9hxy3d8aa90f-1309378747/助眠减压/音乐/lightMusic4.wav",
  type: "助眠"
}, {
  img: "cloud://xiaotiao-6gcr9hxy3d8aa90f.7869-xiaotiao-6gcr9hxy3d8aa90f-1309378747/助眠减压/温柔.jpg",
  name: "温柔",
  src: "cloud://xiaotiao-6gcr9hxy3d8aa90f.7869-xiaotiao-6gcr9hxy3d8aa90f-1309378747/助眠减压/音乐/lightMusic5.wav",
  type: "助眠"
}, {
  img: "cloud://xiaotiao-6gcr9hxy3d8aa90f.7869-xiaotiao-6gcr9hxy3d8aa90f-1309378747/助眠减压/静谧.jpg",
  name: "静谧",
  src: "cloud://xiaotiao-6gcr9hxy3d8aa90f.7869-xiaotiao-6gcr9hxy3d8aa90f-1309378747/助眠减压/音乐/lightMusic6.wav",
  type: "助眠"
}, {
  img: "cloud://xiaotiao-6gcr9hxy3d8aa90f.7869-xiaotiao-6gcr9hxy3d8aa90f-1309378747/助眠减压/空灵.png",
  name: "空灵",
  src: "cloud://xiaotiao-6gcr9hxy3d8aa90f.7869-xiaotiao-6gcr9hxy3d8aa90f-1309378747/助眠减压/音乐/lightMusic7.wav",
  type: "助眠"
} ],

a = wx.createInnerAudioContext();

a.autoplay = !1;
Page({
    data: {             
        isstarted: 0,       
        isbody: 0,
        iscase: 0,
        isrank: 0,
        isSY: !0,
        isTZ: !1,
        isTL: !1,
        isPH: !1,       
        resultData: {
          0: {
            level: "G1",
            desc: "正常或高值",
            fre: "≥1",
            sug: "CKD诊治,缓解症状,保护肾功能"
          },
          1: {
            level: "G2",
            desc: "轻度下降",
            fre: "≥2",
            sug: "缓解CKD进展,降低心血管病风险"
          },
          2: {
            level: "G3a",
            desc: "轻至中度下降",
            fre: "≥3",
            sug: "延缓CKD进展、治疗并发症"
          },
          3: {
            level: "G3b",
            desc: "中至重度下降",
            fre: "≥4",
            sug: "延缓CKD进展、治疗并发症"
          },
          4: {
            level: "G4",
            desc: "重度下降",
            fre: "≥6",
            sug: "综合治疗；透析前准备"
          },
          5: {
            level: "G5",
            desc: "肾衰",
            fre: "≥12",
            sug: "出现尿毒症，替代治疗"
          }
        },  
        calInfo: {
          sex: 1,
          unit: 1
        },     
        tabs: [ "减压", "助眠" ],
        current: 0,
        showMSG: !1,
        today_msg: "",
        today_title: "",
        isPlay: !1,
        selftest: 0,
        level: ""
    },
  
    bindViewTap: function () {
      wx.navigateTo({
        url: "../principle/principle"
      });
    },
    bindage: function (e) {
      this.data.age = e.detail.value;
    },
    bindcnum: function (e) {
      this.data.cnum = e.detail.value;
    },
    bindnum: function (e) {
      this.data.num = e.detail.value;
    },

    chooseSex: function (a) {
      this.data.calInfo.sex = a.detail.value;
    },
    chooseUnit: function (a) {
      this.data.calInfo.unit = a.detail.value;
    },
    caculateScr: function (a) {
      var t;
      return 1 == this.data.calInfo.sex ? a <= .9 ? (a /= .9, (t = 141 * Math.pow(a, .074) * Math.pow(.993, this.data.age)) <= 15 ? 141 * Math.pow(a, -.411) * Math.pow(.993, this.data.age) : t) : (a /= .9,
        (t = 141 * Math.pow(a, -1.057) * Math.pow(.993, this.data.age)) <= 15 ? 141 * Math.pow(a, -1.209) * Math.pow(.993, this.data.age) : t) : a <= .7 ? (a /= .7,
        (t = 144 * Math.pow(a, .156) * Math.pow(.993, this.data.age)) <= 15 ? 144 * Math.pow(a, -.329) * Math.pow(.993, this.data.age) : t) : (a /= .7,
        (t = 144 * Math.pow(a, -1.057) * Math.pow(.993, this.data.age)) <= 15 ? 144 * Math.pow(a, -1.209) * Math.pow(.993, this.data.age) : t);
    },
    caculateScysC: function (a) {
      var t;
      return 1 == this.data.calInfo.sex ? a <= .8 ? (a /= .8, t = 133 * Math.pow(a, -.499) * Math.pow(.996, this.data.age)) : (a /= .8,
        (t = 120 * Math.pow(a, -.825) * Math.pow(.996, this.data.age)) <= 15 ? 133 * Math.pow(a, -1.328) * Math.pow(.996, this.data.age) : t) : a <= .8 ? (a /= .8,
        133 * Math.pow(a, -.499) * Math.pow(.996, this.data.age) * .932) : (a /= .8, (t = 134 * Math.pow(a, -.899) * Math.pow(.996, this.data.age) * .874) <= 15 ? 133 * Math.pow(a, -1.328) * Math.pow(.996, this.data.age) * .932 : t);
    },
    caculateScrScysC: function (a, t) {
      var e;
      return 1 == this.data.calInfo.sex ? a <= .9 ? t <= .8 ? (a /= .9, t /= .8, e = 135 * Math.pow(a, -.207) * Math.pow(t, -.375) * Math.pow(.995, this.data.age)) : (a /= .9,
        t /= .8, (e = 127 * Math.pow(a, -.232) * Math.pow(t, -.414) * Math.pow(.995, this.data.age)) <= 15 ? 135 * Math.pow(a, -.207) * Math.pow(t, -.711) * Math.pow(.995, this.data.age) : e) : t <= .8 ? (a /= .9,
        t /= .8, 135 * Math.pow(a, -.601) * Math.pow(t, -.375) * Math.pow(.995, this.data.age)) : (a /= .9,
        t /= .8, (e = 129 * Math.pow(a, -.358) * Math.pow(t, -.459) * Math.pow(.995, this.data.age)) < 15 ? 135 * Math.pow(a, -.601) * Math.pow(t, -.711) * Math.pow(.995, this.data.age) : e) : a <= .7 ? t <= .8 ? (a /= .7,
        t /= .8, 130 * Math.pow(a, -.248) * Math.pow(t, -.375) * Math.pow(.995, this.data.age)) : (a /= .7,
        t /= .8, (e = 129 * Math.pow(a, .158) * Math.pow(t, -.692) * Math.pow(.995, this.data.age)) <= 15 ? 130 * Math.pow(a, -.248) * Math.pow(t, -.711) * Math.pow(.995, this.data.age) : e) : t <= .8 ? (a /= .7,
        t /= .8, 130 * Math.pow(a, -.601) * Math.pow(t, -.375) * Math.pow(.995, this.data.age)) : (a /= .7,
        t /= .8, (e = 119 * Math.pow(a, -.424) * Math.pow(t, -.463) * Math.pow(.995, this.data.age)) <= 15 ? 130 * Math.pow(a, -.601) * Math.pow(t, -.711) * Math.pow(.995, this.data.age) : e);
    },
    caculateResult: function () {
      if (this.data.age)
        if (this.data.num || this.data.cnum) {
          var a = null;
          if (this.data.num && this.data.cnum) {
            var t = this.data.num;
            1 == this.data.calInfo.unit && (t /= 88.4), a = this.caculateScrScysC(t, this.data.cnum);
          } else if (this.data.num) {
            t = this.data.num;
            1 == this.data.calInfo.unit && (t /= 88.4), a = this.caculateScr(t);
          } else a = this.caculateScysC(this.data.cnum);
          var e = 0;
          a < 90 && a >= 60 ? e = 1 : a < 60 && a >= 45 ? e = 2 : a < 45 && a >= 30 ? e = 3 : a < 30 && a >= 15 ? e = 4 : a < 15 && (e = 5),
            this.setData({
              resultNum: a.toFixed(2),
              resultLevel: this.data.resultData[e].level,
              resultDesc: this.data.resultData[e].desc,
              resultFre: this.data.resultData[e].fre,
              resultSug: this.data.resultData[e].sug,
              resultShow: !0
            });
        } else wx.showToast({
          title: "请填写血清肌酐数或血清胱抑素C数",
          icon: "none"
        });
      else wx.showToast({
        title: "请填写您的年龄",
        icon: "none"
      });
      let that=this
   
      that.setData({
        selftest:1
      })

      that.setData({
        level:this.data.resultData[e].level,
      })
      console.log("看一下selftest",that.data.selftest)
      if(that.data.selftest == 1){
      
        //为勋章墙添加记录
        recordsDB.where({
          _openid:app.globalData.openid
        }).update({
          data:{
            selftest:1
          }
        }),
        
        //为user表添加等级
        userDB.where({
          _openid:app.globalData.openid
        }).update({
          data:{
            level:that.data.level
          }
        })
      }
    },

    bindTapSY: function() {
        this.setData({
            isstarted: 0,
            isbody: 0,
            iscase: 0,
            isrank: 0,
            isSY: !0,
            isTZ: !1,
            isTL: !1,
            isPH: !1
        }), wx.showShareMenu({
            withShareTicket: !1
        });
    },
    bindTapTZ: function() {
        this.setData({
            isbody: 1,
            isstarted: 1,
            iscase: 0,
            isrank: 0,
            isTZ: !0,
            isSY: !1,
            isTL: !1,
            isPH: !1
        })
    },

    bindTapPH: function() {
        this.setData({
            isrank: 1,
            isstarted: 1,
            iscase: 0,
            isbody: 0,
            isSY: !1,
            isTZ: !1,
            isTL: !1,
            isPH: !0
        })
        // wx.navigateTo({
        //   url: '/pages/HADS/HADS',
        // })
    },
   
    toHADS(){
      wx.navigateTo({
        url:'/pages/HADS/HADS',
      })
    },
  
 
  onLoad: function(a) {      
  },
    bindChange: function(e) {
        var t = e.currentTarget.dataset.num;
        console.log(t);
        var o = this.data.testresult.TestScores.filter(function(e) {
            return e.num == t;
        });
        1 == o.length && wx.setStorageSync("TestType", o[0].TestType);
    },

   
  tabSelect: function(a) {
    var e = [], c = a.currentTarget.dataset.id;
      
    if (0 == c) for (var p = 0; p < o.length; p++) "减压" == o[p].type && e.push(o[p]);
    if (1 == c) for (p = 0; p < o.length; p++) "助眠" == o[p].type && e.push(o[p]);
    this.setData({
      current: c,
      dataList: e
    });
  },
  
  stop: function() {
      a.stop(), this.setData({
          isPlay: !1
      }), console.log("音乐已经停止播放");
  },
  click: function(o) {
      var t = o.currentTarget.dataset.id, e = this.data.dataList[t];
      a.src = e.src, a.loop = !0, a.play(), this.setData({
          isPlay: !0
      }), console.log("开始播放音乐索引为：" + t);
  },
  
  selftest(){
   
  },

  onReady: function() {},
  audioPlay: function() {
    a.play();
  },
  audioPause: function() {
      a.pause();
  },
  audio14: function() {
      a.seek(14);
  },
  audioStart: function() {
      a.seek(0);
  },
  onShow: function() {        
  },
  onHide: function() {},
  onUnload: function() {},
  onPullDownRefresh: function() {
        this.refreshtestdata();
  },
  onReachBottom: function() {},
  onShareAppMessage: function() {
        
  }
});